var Define = require("../../../lib/Define.js")

var _activityId = 0
var __sharedTitle = ""
var _userInfo = {}
var _isSign = false     //是否已报名

Page({
  data: {
      data:{},
      sign_btn_class:"bottom-right"
  },
  onLoad:function(options){
      _activityId = options.activityId
        
      if(_activityId){
          this.requestData()
      }
  },
  onShow:function(){
    if(!_isSign){
      _userInfo = Define.getStoredUserInfo()
      this.reqIsSign()
    }
  },
  reqIsSign:function(){
    //请求是否报名
    var self = this

    if(!_userInfo){
      return
    }
    
    wx.request({
      url: Define.value.baseUrl + "/app/activity/issign",
      data: {
        bid: Define.value.bid,
        userId: _userInfo.id,
        activityId: _activityId
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success

        _isSign = res.data.data
        if (_isSign) {
          self.setData({
            sign_btn_class: "bottom-right-disable"
          })
        }
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
  },
  requestData:function(){
      var self = this

    wx.request({
      url: Define.value.baseUrl+"/activity/detail?id="+_activityId,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        var data = res.data
        
        if(data.code == 200){

            data.data.startDateStr = Define.dateFrom(data.data.startDate,"yyyy-MM-dd")
            data.data.endDateStr = Define.dateFrom(data.data.endDate,"yyyy-MM-dd")

            self.setData({
                data:data.data
            })

            __sharedTitle = data.data.title
        }

      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  },
  msgsend:function(){
    wx.navigateTo({
      url: '../../sendmsg/sendmsg',
      success: function(res){
        // success
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  },
  signaction:function(){
    var self = this

    if(_isSign){
      return
    }

    if (!_userInfo) {
      wx.navigateTo({
        url: '/pages/setphone/setphone'
      })
      return
    }

    wx.request({
      url: Define.value.baseUrl + '/app/activity/sign',
      data: {
        bid: Define.value.bid,
        userId: _userInfo.id,
        activityId: _activityId
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        var data = res.data

        _isSign = true
        self.setData({
          sign_btn_class: "bottom-right-disable"
        })

        wx.showModal({
          title: "报名成功",
          content: '是否联系小蜜？',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../sendmsg/sendmsg'
              })

            } else if (res.cancel) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })

  },
  onShareAppMessage: function () {
    return {
      title: __sharedTitle,
      path: '/pages/activity/detail/activityDetail?activityId='+_activityId,
      success: function(res) {
        // 分享成功
      },
      fail: function(res) {
        // 分享失败
      }
    }
  }
})